package com.zbiti.anvil.basic.module.business.service.event;

import com.zbiti.anvil.basic.module.business.api.domain.SamplesLeaveDomain;
import com.zbiti.anvil.basic.module.business.api.service.ISamplesLeaveService;
import com.zbiti.anvil.wf.api.annotation.WfSupported;
import com.zbiti.anvil.wf.api.binder.IWfEventBinder;
import com.zbiti.anvil.wf.api.service.IWfService;
import com.zbiti.anvil.wf.engine.delegate.event.ActivityEvent;
import com.zbiti.anvil.wf.engine.delegate.event.EngineEventType;
import com.zbiti.anvil.wf.engine.delegate.event.EntityEvent;
import com.zbiti.anvil.wf.engine.entity.TaskEntity;
import com.zbiti.anvil.wf.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



/**
 * 流程监听事件
 *
 * @author Fanyc
 * @date 2021-02-18 17:58
 */
@WfSupported(patternMatch = true,wfCodes = "")
public class SamplesLeaveEventBinder implements IWfEventBinder {
    /**
     * 处理任务审批完成
     *
     * @param isApprove         审批结果
     * @param businessKey       业务Key
     * @param taskDefinitionKey 节点定义KEY
     * @param taskEntity          任务对象
     * @return 是否已处理
     */
    @Override
    public boolean handleTaskComplete(boolean isApprove, String businessKey, String taskDefinitionKey, TaskEntity taskEntity) {
        log.info("======================handleTaskComplete===============================");

        return IWfEventBinder.super.handleTaskComplete(isApprove, businessKey, taskDefinitionKey, taskEntity);
    }

    private static final Logger log = LoggerFactory.getLogger(SamplesLeaveEventBinder.class);


    /**
     * 流程服务
     */
    final private IWfService wfService;

    /**
     * 业务服务
     */
    private final ISamplesLeaveService samplesLeaveService;

    /**
     * 处理事件分发
     *
     * @param processInstance 流程实例
     * @param event           事件对象
     * @return 是否已处理
     */
    @Override
    public boolean handleOtherEvent(ProcessInstance processInstance, ActivityEvent event) {
        log.info("======================onActivityEvent===============================");
        // 获取业务Key
        String businessKey = processInstance != null ? processInstance.getBusinessKey() : null;
        if (EngineEventType.ACTIVITY_CANCELLED == event.getType()) {
            return this.handleActivityCancelEvent(businessKey,processInstance, event);
        } else if (EngineEventType.MULTI_INSTANCE_ACTIVITY_STARTED == event.getType()) {
            log.warn("======================会签，任务启动！！！！===============================");
            return true;
        }else if(EngineEventType.ACTIVITY_STARTED == event.getType()){

            log.warn("==========111============ACTIVITY_STARTED任务启动！！！！===============================");
            log.warn("==========111============"+event.getActivityName()+"===============================");
            return true;
        } else if(EngineEventType.TASK_CREATED == event.getType()){

            log.warn("==========111============TASK_CREATED任务启动！！！！===============================");
            log.warn("==========111============"+event.getActivityName()+"===============================");
            return true;
        } else if (EngineEventType.PROCESS_CREATED == event.getType()) {
            log.warn("==========333============流程创建！！！！===============================");
            return true;
        } else if (EngineEventType.ENGINE_CREATED == event.getType()) {
            log.warn("==========444============引擎创建！！！！===============================");
            return true;
        } else {
            return false;
        }
    }

    /**
     * 处理其他类型事件
     *
     * @param processInstance 流程实例
     * @param event           事件对象
     * @return 是否已处理
     */
    @Override
    public boolean handleOtherEvent(ProcessInstance processInstance, EntityEvent event) {

        if (EngineEventType.ENTITY_DELETED == event.getType()){
            log.warn("======================任务被删除===============================");
            return true;
        } else if (EngineEventType.PROCESS_CREATED == event.getType()) {
            log.warn("==========222============流程创建！！！！===============================");
            log.warn("==========222============"+event.getExecutionId()+"===============================");
        } else if (EngineEventType.PROCESS_CREATED == event.getType()) {
            log.warn("======================流程创建！！！！======================333=========");
            return true;
        } else if(EngineEventType.TASK_CREATED == event.getType()){

            log.warn("==========11122============TASK_CREATED任务启动！！！！===============================");
            log.warn("==========11122============"+event.getExecutionId()+"===============================");
            return true;
        }else if (EngineEventType.ENGINE_CREATED == event.getType()) {
            log.warn("======================引擎创建！！！！=================444==============");
            return true;
        }
        return false;
    }

    /**
     * 处理事件分发
     *
     * @param processInstance 流程实例
     * @param event           事件对象
     * @return 是否已处理
     */
    @Override
    public boolean onEntityEvent(ProcessInstance processInstance, EntityEvent event) {

        // 获取业务Key
        String businessKey = event.getBusinessKey(processInstance);
        if (EngineEventType.MULTI_INSTANCE_ACTIVITY_STARTED == event.getType()){
            log.warn("======================会签，任务启动！！！！===============================");
        }
        if (EngineEventType.TASK_COMPLETED == event.getType()) {
            return this.handleActivityCompletedEvent(businessKey, processInstance, event);
        } else if (EngineEventType.PROCESS_COMPLETED == event.getType()) {
            return this.handleProcessCompletedEvent(businessKey,processInstance, event);
        } else {
            return this.handleOtherEvent(processInstance, event);
        }
    }

    /**
     * 构造函数
     *
     * @param wfService 流程服务
     */
    public SamplesLeaveEventBinder(IWfService wfService, ISamplesLeaveService samplesLeaveService) {
        this.wfService = wfService;
        this.samplesLeaveService = samplesLeaveService;
    }

    /**
     * 获取流程服务对象
     *
     * @return 流程服务对象
     */
    @Override
    public IWfService getWfService() {
        return this.wfService;
    }

    /**
     * 业务服务
     *
     * @return 业务服务
     */
    public ISamplesLeaveService getSamplesLeaveService() {
        return samplesLeaveService;
    }

    /**
     * 流程取消事件
     *
     * @param businessKey       业务Key
     * @param processInstanceId 流程实例ID
     * @return 是否已处理
     */
    @Override
    public boolean handleProcessCancel(String businessKey, String processInstanceId) {
        SamplesLeaveDomain updateParam = new SamplesLeaveDomain();
        updateParam.setApplyId(Long.parseLong(businessKey));
        updateParam.setStatus("04");
        this.getSamplesLeaveService().updateById(updateParam);
        return true;
    }

    /**
     * 流程执行流结束事件
     *
     * @param businessKey       业务Key
     * @param processInstanceId 流程实例ID
     * @return 是否已处理
     */
    @Override
    public boolean handleProcessCompleted(String businessKey, String processInstanceId) {
        log.info("======================handleProcessCompleted===============================");
        // 流程结束事件不处理
        return false;
    }

    /**
     * 任务审批通过事件
     *
     * @param businessKey       业务Key
     * @param taskDefinitionKey 节点定义KEY
     * @param taskEntity          任务对象
     * @return 是否已处理
     */
    @Override
    public boolean handleTaskCompleteApproved(String businessKey, String taskDefinitionKey, TaskEntity taskEntity) {
        log.info("======================handleTaskCompleteApproved===============================");
        return false;
    }

    /**
     * 任务审批驳回事件
     *
     * @param businessKey       业务Key
     * @param taskDefinitionKey 节点定义KEY
     * @param taskEntity          任务对象
     * @return 是否已处理
     */
    @Override
    public boolean handleTaskCompleteRejected(String businessKey, String taskDefinitionKey, TaskEntity taskEntity) {
        return false;
    }

//    /**
//     * 任务审批通过事件
//     *
//     * @param businessKey       业务Key
//     * @param taskDefinitionKey 节点定义KEY
//     * @param taskEntity        任务对象
//     * @return 是否已处理
//     */
//    @Override
//    public boolean handleTaskCompleteApproved(String businessKey, String taskDefinitionKey, TaskEntity taskEntity) {
//        /*
//         * 如果需要根据节点做差异性处理，可以在流程定义的时候指定有意义的节点ID，
//         * 然后通过taskDefinitionKey参数来区分
//         */
//
//        AssertUtils.assertNotEmpty(businessKey, "业务Key不能为空");
//        // 审批通过，设置状态
//        SamplesLeaveDomain updateParam = new SamplesLeaveDomain();
//        updateParam.setApplyId(Long.parseLong(businessKey));
//        // 判断当前节点时部门负责人还是部门经理
//        if ("DeptManager".equals(taskDefinitionKey)) {
//            updateParam.setStatus("05");
//        } else if ("DeptAdmin".equals(taskDefinitionKey)) {
//            updateParam.setStatus("06");
//        } else if ("UserTask3".equals(taskDefinitionKey)) {
//            updateParam.setStatus("07");
//        }
//        if (StringUtils.isNotEmpty(updateParam.getStatus())) {
//            this.getSamplesLeaveService().updateById(updateParam);
//            return true;
//        }
//        return false;
//    }
//
//    /**
//     * 任务审批驳回事件
//     *
//     * @param businessKey       业务Key
//     * @param taskDefinitionKey 节点定义KEY
//     * @param taskEntity        任务对象
//     * @return 是否已处理
//     */
//    @Override
//    public boolean handleTaskCompleteRejected(String businessKey, String taskDefinitionKey, TaskEntity taskEntity) {
//        /*
//         * 如果需要根据节点做差异性处理，可以在流程定义的时候指定有意义的节点ID，
//         * 然后通过taskDefinitionKey参数来区分
//         */
//
//        AssertUtils.assertNotEmpty(businessKey, "业务Key不能为空");
//        // 根据不同的审批节点，设置不同的状态
//        SamplesLeaveDomain updateParam = new SamplesLeaveDomain();
//        updateParam.setApplyId(Long.parseLong(businessKey));
//        updateParam.setStatus("03");
//        this.getSamplesLeaveService().updateById(updateParam);
//        return true;
//    }
}
